package com.cmti.skeleton.dao.mapper;

import com.cmti.skeleton.dto.SysRoleDto;
import com.cmti.skeleton.entity.SysRole;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

public interface SysRoleMapper {

    @Delete({
        "delete from sys_role",
        "where ID = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into sys_role (NAME, STATUS, ",
        "REMARK)",
        "values (#{name,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER}, ",
        "#{remark,jdbcType=VARCHAR})"
    })
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
    int insert(SysRole record);

    @InsertProvider(type = SysRoleSqlProvider.class, method = "insertSelective")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
    int insertSelective(SysRole record);

    @Select({
        "select",
        "ID, NAME, STATUS, REMARK",
        "from sys_role",
        "where ID = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column = "ID", property = "id", jdbcType = JdbcType.BIGINT, id = true)
        ,
        @Result(column = "NAME", property = "name", jdbcType = JdbcType.VARCHAR)
        ,
        @Result(column = "STATUS", property = "status", jdbcType = JdbcType.INTEGER)
        ,
        @Result(column = "REMARK", property = "remark", jdbcType = JdbcType.VARCHAR)
    })
    SysRole selectByPrimaryKey(Long id);

    @SelectProvider(type = SysRoleSqlProvider.class, method = "selectListByDto")
    List<SysRoleDto> selectListByDto(SysRoleDto dto);

    @Select({
        "SELECT ss.* FROM sys_role ss",
        "inner join sys_user_role sr on ss.id=sr.ROLE_ID",
        "WHERE sr.USER_ID=#{userId}"
    })
    List<SysRole> selectByUserId(Long userId);

    @UpdateProvider(type = SysRoleSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(SysRole record);

    @Update({
        "update sys_role",
        "set NAME = #{name,jdbcType=VARCHAR},",
        "STATUS = #{status,jdbcType=INTEGER},",
        "REMARK = #{remark,jdbcType=VARCHAR}",
        "where ID = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(SysRole record);

    @Select({"SELECT * FROM sys_role where id = #{roleId}"})
    SysRole selectById(Long roleId);
}
